<?php
if(!defined('IN_SITE')) {
	exit('Access Denied');
}

class tools_module_field{
	public $info_array = array();
	public $page_array = array();
	public $tree_array = array();

	static function &instance() {
		static $object;
		if(empty($object)) {
			$object = new self();
		}
		return $object;
	}
	/*
	*	取出一条信息
	*/
	public function one_info($tmf_id='') {
		$info = array();
		if($tmf_id){
			$sql = "SELECT * FROM ".DB::table('tools_module_field')." WHERE tmf_id='".$tmf_id."' LIMIT 1";
			$info = DB::fetch_first($sql);	
		}
		return $info;
	}
    
	/*
	*  添加系统信息
	*/
	public function add() {
        global $_G;
        //DEBUG 接收并获取数据表所属模块名称 开始
        $tm_id = isset($_REQUEST['tm_id']) ? $_REQUEST['tm_id'] : '';
        $tools_module_class_path = libfile('tools/module', 'class','..','/admin');
        if(file_exists($tools_module_class_path) && $tm_id){
			require_once $tools_module_class_path;
			$tools_module = new tools_module();
            $tools_module_info = $tools_module->one_info($tm_id);
            if($tools_module_info['tm_description']){
                $tm_name = $tools_module_info['tm_description'].'('.$tools_module_info['tm_name'].')';
            }else{
                $tm_name = '请选择模块';
            }
		}
        //DEBUG 接收并获取数据表所属模块名称 结束
        //DEBUG 接收并获取字段所属所属数据表名称 开始
        $tmt_id = isset($_REQUEST['tmt_id']) ? $_REQUEST['tmt_id'] : '';
        $tools_module_table_class_path = libfile('tools/module_table', 'class','..','/admin');
        if(file_exists($tools_module_table_class_path) && $tmt_id){
			require_once $tools_module_table_class_path;
			$tools_module_table = new tools_module_table();
            $tools_module_table_info = $tools_module_table->one_info($tmt_id);
            if($tools_module_table_info['tmt_name']){
                $tmt_name = $tools_module_table_info['tmt_name'].'('.$tools_module_table_info['tmt_table_name'].')';
            }else{
                $tmt_name = '请选择模块';
            }
		}
        //DEBUG 接收并获取字段所属所属数据表名称 结束
		$issubmit = isset($_REQUEST['issubmit']) && !empty($_REQUEST['issubmit']) ? $_REQUEST['issubmit']:'';
		$tools_module_field = isset($_REQUEST['tools_module_field']) && !empty($_REQUEST['tools_module_field']) ? $_REQUEST['tools_module_field']:'';
		if('1'==$issubmit){
            //TODO 后端字段校验
            $tools_module_field['create_dateline'] = TIMESTAMP;
            $insert_id = DB::insert('tools_module_field',$tools_module_field,true);
            if($insert_id){
                showmessage('操作成功','index.php?mod=tools&action=module_field&do=index&tm_id='.$tm_id.'&tmt_id='.$tmt_id);
            }else{
                showmessage('操作失败或信息未发生变更','index.php?mod=tools&action=module_field&do=index&tm_id='.$tm_id.'&tmt_id='.$tmt_id);
            }
		}else{
            //DEBUG 字段UI显示类型
            $tmf_type_info=$this->tmf_type();
			include template('tools/module_field/add');
		}
	}
    
	/*
	*  编辑系统信息
	*/
	public function edit() {
        global $_G;
		$issubmit = isset($_REQUEST['issubmit']) && !empty($_REQUEST['issubmit']) ? $_REQUEST['issubmit']:'';
		$tools_module_field = isset($_REQUEST['tools_module_field']) && !empty($_REQUEST['tools_module_field']) ? $_REQUEST['tools_module_field']:'';
        $tmf_id = isset($_REQUEST['tmf_id']) ? $_REQUEST['tmf_id'] : '';
        if($tmf_id){
            if('1'==$issubmit){
                $tools_module_field['modify_dateline'] = TIMESTAMP;
                $where = array('tmf_id'=>$tmf_id);
                $effect_row = DB::update('tools_module_field',$tools_module_field,$where);
                if($effect_row){
                    showmessage('操作成功','index.php?mod=tools&action=module_field&do=index&tm_id='.$tools_module_field['tm_id'].'&tmt_id='.$tools_module_field['tmt_id']);
                }else{
                    showmessage('操作失败','index.php?mod=tools&action=module_field&do=index&tm_id='.$tools_module_field['tm_id'].'&tmt_id='.$tools_module_field['tmt_id']);
                }
            }else{
                //DEBUG 获取节点信息
                $info = self::one_info($tmf_id);
                //DEBUG 字段UI显示类型
                $tmf_type_info=$this->tmf_type();
                //DEBUG 接收并获取数据表所属模块名称 开始
                $tm_id = $info['tm_id'];
                $tools_module_class_path = libfile('tools/module', 'class','..','/admin');
                if(file_exists($tools_module_class_path) && $tm_id){
                    require_once $tools_module_class_path;
                    $tools_module = new tools_module();
                    $tools_module_info = $tools_module->one_info($tm_id);
                    if($tools_module_info['tm_description']){
                        $tm_name = $tools_module_info['tm_description'].'('.$tools_module_info['tm_name'].')';
                    }else{
                        $tm_name = '请选择模块';
                    }
                }
                //DEBUG 接收并获取数据表所属模块名称 结束
                //DEBUG 接收并获取字段所属所属数据表名称 开始
                $tmt_id = $info['tmt_id'];
                $tools_module_table_class_path = libfile('tools/module_table', 'class','..','/admin');
                if(file_exists($tools_module_table_class_path) && $tmt_id){
                    require_once $tools_module_table_class_path;
                    $tools_module_table = new tools_module_table();
                    $tools_module_table_info = $tools_module_table->one_info($tmt_id);
                    if($tools_module_table_info['tmt_name']){
                        $tmt_name = $tools_module_table_info['tmt_name'].'('.$tools_module_table_info['tmt_table_name'].')';
                    }else{
                        $tmt_name = '请选择模块';
                    }
                }
                //DEBUG 接收并获取字段所属所属数据表名称 结束
                include template('tools/module_field/edit');
            }
        }else{
            showmessage('请选择编辑对象','index.php?mod=tools&action=module&do=index');
        }
	}
    
	/*
	*  删除系统信息
	*/
	public function delete() {
        $tm_id = isset($_REQUEST['tm_id']) ? $_REQUEST['tm_id'] : '';
        $tmt_id = isset($_REQUEST['tmt_id']) ? $_REQUEST['tmt_id'] : '';
        $tmf_id = isset($_REQUEST['tmf_id']) ? $_REQUEST['tmf_id'] : '';
        if($tmf_id){
            DB::delete("tools_module_field", array("tmf_id" => $tmf_id), $limit=1);
            //TODO 逻辑删需要创建isdelete字段 0=未删除 1=已删除
            //DB::update("tools_module_field",array('isdelete'=>1), array("tmf_field_name" => $tmf_field_name), $limit=1);
            showmessage('操作成功','index.php?mod=tools&action=module_field&do=index&tm_id='.$tm_id.'&tmt_id='.$tmt_id);
        }
    }
	/*
	*  系统信息列表
	*/
	public function index(){
        global $_G;
        $page = max(1, intval($_GET['page']));
        $perpage = $limit = 20;
        $start=(($page-1) * $perpage);
        $wheresql = "";
        $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
        if($keyword){
            $wheresql = " AND keyword LIKE '%".$keyword."%'";
        }
        $tm_id = isset($_REQUEST['tm_id']) ? $_REQUEST['tm_id'] : '';
        if($tm_id){
            $wheresql .= " AND tm_id = '".$tm_id."' ";
        }
        $tmt_id = isset($_REQUEST['tmt_id']) ? $_REQUEST['tmt_id'] : '';
        if($tmt_id){
            $wheresql .= " AND tmt_id = '".$tmt_id."' ";
        }
        $sql_info = "SELECT * FROM ".DB::table('tools_module_field')." WHERE 1=1 ".$wheresql." ORDER BY tmf_id DESC ".DB::limit($start, $limit);
        $sql_info_result = DB::fetch_all($sql_info);
        //DEBUG 获取所属模块与所属表名称 开始
        $tools_module_class_path = libfile('tools/module', 'class','..','/admin');
        if(file_exists($tools_module_class_path)){
			require_once $tools_module_class_path;
			$tools_module = new tools_module();
		}
        $tools_module_table_class_path = libfile('tools/module_table', 'class','..','/admin');
        if(file_exists($tools_module_table_class_path)){
			require_once $tools_module_table_class_path;
			$tools_module_table = new tools_module_table();
		}
        foreach($sql_info_result AS $key => $value){
            //DEBUG 模块名称
            $tools_module_info = $tools_module->one_info($value['tm_id']);
            if($tools_module_info['tm_description']){
                $tm_name = $tools_module_info['tm_description'].'('.$tools_module_info['tm_name'].')';
            }else{
                $tm_name = '请选择模块';
            }
            $sql_info_result[$key]['tm_name']=$tm_name;
            //DEBUG 表名称
            $tools_module_table_info = $tools_module_table->one_info($value['tmt_id']);
            if($tools_module_table_info['tmt_name']){
                $tmt_name = $tools_module_table_info['tmt_name'].'('.$tools_module_table_info['tmt_table_name'].')';
            }else{
                $tmt_name = '请选择数据表';
            }
            $sql_info_result[$key]['tmt_name']=$tmt_name;
            //DEBUG 字段UI显示类型
            $tmf_type_info=$this->tmf_type();
            $sql_info_result[$key]['tmf_type_name']=$tmf_type_info[$value['tmf_type']];
        }
        //DEBUG 获取所属模块与所属表名称 结束
        $sql_total_rows = "SELECT count(*) FROM ".DB::table('tools_module_field')." WHERE 1=1 ".$wheresql."";
        $sql_total_rows_result = DB::result_first($sql_total_rows);
        $multipage = multi($sql_total_rows_result, $perpage, $page, "index.php?mod=tools&action=module_field&do=index&tm_id=".$tm_id.'&tmt_id='.$tmt_id);
        //DEBUG 字段UI显示类型
        $tmf_type_info=$this->tmf_type();
		include template('tools/module_field/index');	
	}
    /**
     * 字段显示形式
     * @param $tmf_type int
     * @return string 类型名称
     */
    public function tmf_type($tmf_type=''){
        $tmf_type_array = array(
            'input'=>'文本输入框 (input)',
            'textarea'=>'纯文本编辑框 (textarea)',
            'air_textarea'=>'富媒体编辑框 (textarea)',
            'select'=>'下拉框 (select)',
            'radio'=>'单选框 (radio)',
            'checkbox'=>'多选框 (checkbox)',
            ''=>'不显示'
        );
        if($tmf_type){
            return $tmf_type_array[$tmf_type];
        }else{
            return $tmf_type_array;
        }
    }
    
   /**
     * 接口 AJAX或外部请求检索数据
     *
     * @param $return_type string json/xml.
     *
     * @return print string json/xml.
     */
	public function api(){
        $return = '';
        $page = max(1, intval($_GET['page']));
        $perpage = $limit = 20;
        $start=(($page-1) * $perpage);
        $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
        $return_type = isset($_REQUEST['return_type']) ? $_REQUEST['return_type'] : 'json';
        $wheresql = "";
        if($keyword){
            $wheresql = " WHERE title LIKE '%".$keyword."%'";
        }
        $sql_info = "SELECT tmf_field_name FROM ".DB::table('tools_module_field')." WHERE 1=1 ".$wheresql." ORDER BY tmf_field_name DESC ".DB::limit($start, $limit);
        $sql_info_result = DB::fetch_all($sql_info);
        if('json'==$return_type){
            $return = json_encode($sql_info_result);	
        }elseif('xml'==$return_type){
            $return = array2xml($sql_info_result);
        }
		echo $return;
	}
    
	/*
	* ajax 返回信息
	*/
	public function return_json($error,$message,$output=1) {
		$return_array = array(
			"error" => $error,
			"message" => $message
		);
		if($output){
			echo json_ext($return_array);
			die;
		}else{
			return json_ext($return_array);
		}
	}
}
?>